using Godot;

public partial class ViewController : Node3D
{
	[ExportGroup("Properties")]
	[Export] private Node3D target;
	[Export] private Camera3D camera;

	[ExportGroup("Zoom")]
	[Export] private float zoomMinimum = 16f;
	[Export] private float zoomMaximum = 4f;
	[Export] private float zoomSpeed = 10f;

	[ExportGroup("Rotation")]
	[Export] private float rotationSpeed = 120f;

	private Vector3 cameraRotation;
	private float zoom = 10f;
	private	Vector3 input = Vector3.Zero;

	public override void _Ready()
	{
		cameraRotation = RotationDegrees;
	}

	public override void _PhysicsProcess(double delta)
	{
		//Set position and rotation to targets
		Position = Position.Lerp(target.Position, (float)delta * 4);
		RotationDegrees = RotationDegrees.Lerp(cameraRotation, (float)delta * 6);

		camera.Position = camera.Position.Lerp(new Vector3(0, 6, zoom), (float)delta * 8);

		HandleInput(delta);
	}

	private void HandleInput(double delta)
	{
		//Rotation
		input.Y = -Input.GetAxis("RightStick_Left", "RightStick_Right");
		input.X = -Input.GetAxis("RightStick_Up", "RightStick_Down");
	
		cameraRotation += input * rotationSpeed * (float)delta;
		cameraRotation.X = Mathf.Clamp(cameraRotation.X, -30, 20);
	
		//Zooming
		zoom += Input.GetAxis("Zoom_In", "Zoom_Out") * zoomSpeed * (float)delta;
		zoom = Mathf.Clamp(zoom, zoomMaximum, zoomMinimum);
	}
}
